// pages/onlinePay/onlinePay.js
const app = getApp()
import api from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: true,
    hasDefault: false,
    isShowExaminee: true,

    examItem: {},

    addressInfoItem: {},

    dafaultAddress: {},
    selectedAddress: {},
    addressInfo:{},

    order_identifier:null
  },

  goToMyInfo(e) {
    wx.navigateTo({
      url: '/pages/myApplyInfo/myApplyInfo',
    })
  },

  goToAddAddressInfo(){
    wx.navigateTo({
      url: '/pages/selectAddress/selectAddress',
    })
  },

  goToSelectAddressInfo(){
    wx.navigateTo({
      url: '/pages/selectAddress/selectAddress',
    })
  },

  goToCreateOrder(){
    if(this.data.isShow){
      wx.showToast({
        title: '请添加收货地址',
        icon: 'none'
      })
      return
    }
    if (this.data.isShowExaminee){
      wx.showToast({
        title: '请选择考试人',
        icon: 'none'
      })
      return
    }

    console.log(app.globalData.addressInfo)
    console.log(app.globalData.examItem)

    let address = {}
    if (this.data.dafaultAddress != {}){
      console.log('dafault')
      address = this.data.dafaultAddress
    } 
    if (this.data.selectedAddress != {}){
      console.log('selected')
      address = this.data.selectedAddress
    }
    console.log(address)
    let examItem = this.data.examItem
    let params = {}


    params.exam_id = examItem.exam_id

    params.buyer_address = address.province_name + address.city_name + address.area_name + address.address
    params.results_name = address.name
    params.results_phone = address.phone

    params.exam_status = 1
    params.total_price = examItem.online_price
    params.buyer_name = this.data.infoItem.real_name
    params.buyer_phone = this.data.infoItem.mobile

  console.log(params)
    api.POST('/applet/exam_order/create', params, res => {
      
      this.setData({
        order_identifier: res.data.exam_order_id
      })
      console.log(res)
      console.log(res.data.exam_order_id)
      this.pay(res.data.exam_order_id)
    })
  },
  // console.log(res)
  //     let obj = {}
  //     obj.order_identifier = res.data.exam_order_id
      
  //     api.POST('/applet/exam_order/checkExamPermit', obj, res => {
  //   // 准考证
  //   console.log(res.data)
  //   app.globalData.card = res.data
  // })

  //     api.POST('/applet/ContactService/detail', {}, res => {
  //   app.globalData.QR = res.data.exam_card_url
  // })

  pay(id) {
    const that = this;
    api.POST('applet/customer/jsApiPay', {
      out_trade_no: id,
      body: "123",
      attach: "123"
    }, res => {
      console.log(res)
      that.requestPayment(res.data)
    })
  },

  requestPayment(payModel) {
    //  获取微信支付的数据
    let that = this;
    wx.requestPayment({
      'timeStamp': payModel.timeStamp,
      'nonceStr': payModel.nonceStr,
      'package': payModel.package,
      'paySign': payModel.paySign,
      'signType': "HMAC-SHA256",
      'success': function (res) {
        console.log('支付成功')
        setTimeout(function(){
          wx.navigateTo({
            url: '/pages/onlineApplySuccess/onlineApplySuccess?pid=' + that.data.order_identifier,
          })
        },500)
        // that.getInit();
      },
      'fail': function (res) {
        console.log('支付失败');
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/myapply/myapply'
          })
        }, 500)
        // wx.showModal({
        //   title: '提示',
        //   content: '支付失败',
        //   showCancel: false,
        //   success: function (res) {
        //     if (res.confirm) {
        //       wx.reLaunch({
        //         url: '../order/order?oid=' + that.data.oid,
        //       })
        //     }
        //   }
        // });
      }
    })
  },

  //默认地址
  getDefault(){
    api.POST('/applet/address/defaultAddress', {}, res => {
      console.log(res)
      if(res.data.status === 2){
        this.setData({
          dafaultAddress: res.data,
          hasDefault: true,
          isShow:false
        })
      } else if (res.data.status === 1){
        this.setData({
          hasDefault: false,
          isShow: true
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.setData({
    //   examItem: app.globalData.examItem
    // })
    // console.log(this.data.examItem)

   
    
    

    this.getDefault()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    if (app.globalData.selectedProvince){
      this.setData({
        addressInfoItem: app.globalData.addressInfo,
        isShow: false
      })
    }

    this.setData({
      examItem: app.globalData.examItem,

      infoItem: app.globalData.infoItem,

      selectedAddress: app.globalData.addressInfoItem
      
    })
    console.log(this.data.infoItem)
    console.log(this.data.selectedAddress)

    if (this.data.infoItem) {
      this.setData({
        isShowExaminee: false
      })
    }
    if (this.data.selectedAddress) {
      this.setData({
        isShow: false,
        hasDefault: false
      })
    }
    


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})